package com.ymy.thinking.in.spring.dependency.source;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

/**
 * 读取外部配置文件
 *
 * @author Ringo
 * @date 2022/2/5 16:46
 */
@Configuration
@PropertySource(value = {"classpath:META-INFO/default.properties"}, encoding = "UTF-8")
public class ExternalConfigurationDependencySourceDemo {

    @Value("${user.id:-1}")
    private Long id;

    @Value("${user.resource:classpath:META-INFO/default.properties}")
    private Resource resource;

    @Value("${usr.name}")
    private String name;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ExternalConfigurationDependencySourceDemo.class);

        applicationContext.refresh();

        applicationContext.getBeanProvider(ExternalConfigurationDependencySourceDemo.class)
                .ifAvailable(demo -> {
                    System.out.println(demo.id);
                    System.out.println(demo.resource);
                    System.out.println(demo.name);
                });

        applicationContext.close();
    }

}
